import { defineStore } from "pinia";
import { Login, Reg } from "../api/user/index";
import { UserType } from "@/api/user/type";
import { ElMessage } from 'element-plus'
import router from "@/router";
import { Local } from "@/utils/storage";
export const useUser = defineStore("userInfo", {
  state: () => {
    return {
      count: 0,
    };
  },

  getters: {},
  actions: {
    // login
    async Login(userInfo: UserType) {
      if (userInfo.username == '' || userInfo.password == "") return
      let { data: { message } } = await Login(userInfo);
      console.log(message);
      if (message != "login success") {
        ElMessage({
          type: "error",
          message: message
        })
      } else {
        ElMessage({
          type: "success",
          message: message
        })
        // 存入token  token  来自后端  在请求拦截也要配置token
        Local.set("token", message)
        router.push("/home")
      }
    },
    // Reg
    async Reg(userInfo: UserType) {
      if (userInfo.username == '' || userInfo.password == "") return
      let { data } = await Reg(userInfo)
      console.log(data);
    },
    // logout
    Logout() {
      // 清除Stroage 数据 token
    }
  },


});
